<?php
require_once 'XML/Unserializer.php';
require_once 'Validate.php';
$rs = $db1->query("select * from config limit 1");
$config = $rs->fetch();

function myinfo()
{
	global $db1;
	$sql = "select * from users where session='$_COOKIE[session]'";
	$rs = $db1->query($sql);
	return $rs->fetch();
}

function myauth()
{
	global $conf;
	if($myinfo = myinfo())return $myinfo;
	else goURL($conf['site_url'], '用户未登录,无权限访问.');
}

$myinfo = myinfo();
//print_r($myinfo);

$sex = array('2'=>'男性','3'=>'女性');

function admin_err_page($msg, $url='')
{
    global $myinfo,$conf,$config,$active;
    if(!$url)$url = $_SERVER['HTTP_REFERER'];
    include template('admin/error');
    exit();
}

function admin_ok_page($msg, $url='')
{
    global $myinfo,$conf,$config,$active;
    if(!$url)$url = $_SERVER['HTTP_REFERER'];
    include template('admin/ok');
    exit();
}


function event_filter($post)
{
    global $db1;
    $rs = $db1->query("select * from event_type where t_name='$post[type]'");
    $et = $rs->fetch();
    if(!$et)return false;
    if($et['show_player']==0)$post['player_id'] = 0;
    if($et['show_info']==0)$post['info'] = '';
    if($et['plus']==0)$post['dkp'] = 0-$post['dkp'];
    if($et['show_dkp']==0)$post['dkp'] = '';
    
    return $post;
}

class RTXMLParser
{
    var $rawxml='';
    var $data=array();

    function RTXMLParser($xml='')
    {
        if($xml!='')$this->decode($xml);
    }

    function decode($xml)
    {
        $unserializer = &new XML_Unserializer();

        // userialize the document
        $status = $unserializer->unserialize($xml, false);

        if (PEAR::isError($status)) {
            echo 'Error: ' . $status->getMessage();
        } else {
            $this->data = $unserializer->getUnserializedData();
            $this->rawxml = $xml;
        }
        return $this->data;
    }

    //基本信息
    function basicInfo()
    {
        if($this->data)
        {
            return array(
                'realm' => $this->data['realm'],
                'start' => $this->data['start'],
                'end' => $this->data['end'],
                'zone' => $this->data['zone'],
                'key' => $this->data['key'],
                'note' => $this->data['note'],
            );
        }
    }

    //参与人员
    function playerInfo()
    {
        $players = array();
        if($this->data)
        {
            foreach($this->data['PlayerInfos'] as $player)
            {
                $players[] = $player;
            }
            
        }
        return $players;
    }

    //Boss击杀
    function bossKills()
    {
        $bosskills = array();
        if($this->data)
        {
            foreach($this->data['BossKills'] as $boss)
            {
                $bosskills[] = array('name'=>$boss['name'],'time'=>$boss['time']);
            }
        }
        return $bosskills;
    }

    //团灭
    function wipes()
    {
        if($this->data)
        {
            return $this->data['Wipes']['Wipe'];
        }
    }

    //人员加入时间
    function joins()
    {
        $joins = array();
        if($this->data)
        {
            foreach($this->data['Join'] as $player)
            {
                $joins[] = $player;
            }
        }
        return $joins;
    }

    //人员离队
    function leaves()
    {
        $leaves = array();
        if($this->data)
        {
            foreach($this->data['Leave'] as $player)
            {
                $leaves[] = $player;
            }
        }
        return $leaves;
    }

    //拾取
    function loots()
    {
        $loots = array();
        if($this->data)
        {
            foreach($this->data['Loot'] as $loot)
            {
                $loots[] = $loot;
            }
        }
        return $loots;
    }
}

class Raid
{
    var $id = false;
    var $info = array();
    var $members = array();
    var $events = array();

    function Raid($id=0)
    {
        global $db1;
        $this->db = $db1;
        if($id)$this->load($id);
    }

    function load($id=0)
    {
        if(!$id)$id = $this->id;
        $rs = $this->db->query("select * from raids where r_id='$id'");
        if($row = $rs->fetch())
        {
            $this->info = $row;
            $this->id = $row['r_id'];
            $this->members = $this->db->query("select * from raid_mdata where raid_id='$row[r_id]'")->fetchAll();
            $this->events = $this->db->query("select * from events_raid where raid_id='$row[r_id]' order by dt")->fetchAll();
        }
    }

    function memidArray()
    {
        $arr = array();
        foreach($this->members as $m)
        {
            $arr[] = $m['player_id'];
        }
        return $arr;
    }

    function rinfo($info)
    {
        $err = '';
        $validate = array(
            'r_title'    => array('type'=>'string', 'min_length'=>2),
            'r_muster_dt'=> array('type'=>'string', 'min_length'=>19),
            'r_start_dt' => array('type'=>'string', 'min_length'=>19),
            'r_end_dt'   => array('type'=>'string', 'min_length'=>19),
	);
	$result = Validate::multiple($info, $validate);
        if(!$result['r_title'])$err .= '请填写活动标题';
        if(!$result['r_muster_dt'])$err .= '/请填写集合时间';
        if(!$result['r_start_dt'])$err .= '/请填写开始时间';
        if(!$result['r_end_dt'])$err .= '/请填写结束时间';
        if(strtotime($info['r_start_dt']) >= strtotime($info['r_end_dt']))$err .= '/结束时间不能小于开始时间';
        if($err!='')admin_err_page($err);
        $post = array(
            'r_title'     => $info['r_title'],
            'dkp_id'      => $info['dkp_id'],
            'rule_id'     => $info['rule_id'],
            'r_muster_dt' => $info['r_muster_dt'],
            'r_start_dt'  => $info['r_start_dt'],
            'r_end_dt'    => $info['r_end_dt'],
            'r_cmt'       => $info['r_cmt'],
        );
        $post = array_map("strip_tags", $post);
        if($this->id)
        {
            $this->db->UpData($post, 'raids', array('r_id'=>$this->id));
            $this->load($this->id);
            //admin_ok_page("修改成功,请不要刷新此页面以免出现剑类专精效果.", "./?admin&raidid=$raid[r_id]");
        }
        else
        {
            $id = $this->db->InsertData($post, 'raids');
            $this->load($id);
            //admin_ok_page("新增成功,请不要刷新此页面以免出现剑类专精效果.", "./?admin/members&raidid=$id");
        }
        return $this->id;
    }

    function changeMembers($members)
    {
        if(!$this->id || !$members)return false;
        $this->db->beginTransaction();
        $old = $this->memidArray();
        $this->db->exec("delete from raid_mdata where raid_id='$this->id'");
        foreach($members as $playerid)
        {
            //$rs = $this->db->query("select count(*) from players where ");
            if(!$playerid)
            {
                $this->db->rollBack();
                return false();
            }
            $post = array(
                'player_id' => $playerid,
                'raid_id'   => $this->id,
                'join_dt'   => $this->info['r_muster_dt'],
                'leave_dt'  => $this->info['r_end_dt'],
                'dkp'       => 0,
            );
            $this->db->InsertData($post, 'raid_mdata');
        }
        foreach($old as $pid)
        {
            if(!in_array($pid, $members))
            {
                $this->db->exec("delete from events_raid where player_id='$pid' and raid_id='$this->id'");
            }
        }
        $this->db->commit();
        $this->load();
        return true;
    }

    function addEvent($post)
    {
        if(!$this->id)return false;
        $post['raid_id'] = $this->id;
        $post['dkp_id'] = $this->info['dkp_id'];
        $eventid = $this->db->InsertData($post, 'events_raid');
        $this->load();
        return $eventid;
    }

    function modifyEvent($post,$eventid)
    {
        if(!$this->id)return false;
        $post['raid_id'] = $this->id;
        $post['dkp_id'] = $this->info['dkp_id'];
        $this->db->UpData($post, 'events_raid', array('re_id'=>$eventid));
        $this->load();
        return true;
    }

    function delEvent($id)
    {
        if(!$this->id)return false;
        $this->db->exec("delete from events_raid where re_id='$id' and raid_id='$this->id'");
        return true;
    }

}

?>